package com.winona;

import java.io.Serializable;

import android.app.ActivityGroup;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.taplinker.client.ActivityListen;
import com.taplinker.client.AppConfigFactory;
import com.taplinker.client.R;
import com.taplinker.client.R.color;
import com.taplinker.client.core.ImageManager;
import com.taplinker.client.im.activity.IMQueueActivity;
import com.taplinker.client.im.domain.ImDataCenter;
import com.winona.bean.User;
import com.winona.code.WinonaApplication;
import com.winona.domain.DataCenter;
import com.winona.domain.MenuDomain;
import com.winona.ui.AcademicCenterActivity;
import com.winona.ui.ConversationActivity;
import com.winona.ui.LoginActivity;
import com.winona.ui.PerToolActivity;
import com.winona.ui.PersonalSettingActivity;
import com.winona.widget.DrawerGarment;

/**
 * 侧边栏
 * 
 * @author djbian
 * 
 */
public class Base1Activity extends ActivityGroup implements ActivityListen {
	protected DrawerGarment mLeftNav;
	protected boolean mInitFlag = false;
	private TextView user_name;
	private TextView home;
	private TextView sensitive;
	private TextView acne;
	private TextView stain;
	private TextView dry;
	private TextView complex;
	private TextView cosmetology;
	private TextView conversation;
	private TextView test;
	private TextView center;
	private ImageView left_img, left_name, left_home, left_sensitive,
			left_acne, left_stain, left_dry, left_complex, left_cosmetology,
			left_conversation, left_test, letf_center, user_img;
	private View ll_home, ll_academic, ll_tool, ll_set, userinfo_area,
			ll_conversation, userinfo;
	private Intent intent;

	private User user;

	private TextView mNavPrivateCount;

	private ScrollView sv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		WinonaApplication.getInstance().addActivity(this);

		mLeftNav = new DrawerGarment(this, R.layout.left_nav1);

		findView();
		initNav();

	}

	private void initNav() {

		ll_home.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				closeMenu();
				if (!MenuDomain.isOpen(MenuDomain.MenuItems.HOME)) {
					MenuDomain.open(MenuDomain.MenuItems.HOME);
					Intent it = new Intent(Base1Activity.this,
							HomeActivity.class);
					it.putExtra("home", "0");
					// ImDataCenter.getInstance().setHome(true);
					startActivity(it);
				}
			}
		});
		ll_conversation.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				closeMenu();

				Intent it = new Intent(Base1Activity.this,
						IMQueueActivity.class);
				// it.putExtra("conversation", "0");
				// ImDataCenter.getInstance().setSensitive(false);
				startActivity(it);

			}
		});
		ll_academic.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				closeMenu();

				Intent it = new Intent(Base1Activity.this,
						AcademicCenterActivity.class);
				it.putExtra("academic", "0");
				startActivity(it);

			}
		});
		ll_tool.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				closeMenu();

				Intent it = new Intent(Base1Activity.this,
						PerToolActivity.class);
				it.putExtra("tool", "0");
				startActivity(it);

			}
		});
		ll_set.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				closeMenu();

				Intent it = new Intent(Base1Activity.this,
						PersonalSettingActivity.class);
				it.putExtra("set", "0");
				startActivity(it);

			}
		});

	}

	private void findView() {

		// 整个区域
		userinfo_area = findViewById(R.id.left_userinfo_area);
		ll_home = mLeftNav.findViewById(R.id.left_ll_home);
		ll_conversation = mLeftNav.findViewById(R.id.left_ll_conversation);
		ll_academic = mLeftNav.findViewById(R.id.left_ll_academic);
		ll_tool = mLeftNav.findViewById(R.id.left_ll_tool);
		ll_set = mLeftNav.findViewById(R.id.left_ll_set);

		user_name = (TextView) mLeftNav.findViewById(R.id.left_user_name);
		user_img = (ImageView) mLeftNav.findViewById(R.id.left_user_img);
		left_home = (ImageView) mLeftNav.findViewById(R.id.left_home_img);
		home = (TextView) mLeftNav.findViewById(R.id.left_home_name);

		mNavPrivateCount = (TextView) mLeftNav
				.findViewById(R.id.nav_private_count);

		if (!TextUtils.isEmpty(AppConfigFactory.getInstance().getExpert()
				.getHead())) {
			ImageManager.getInstance().loadImage(
					ConstantValue.IMAGE
							+ AppConfigFactory.getInstance().getExpert()
									.getHead(), user_img, 60, 60, null);
		}

		user_name.setText(AppConfigFactory.getInstance().getUserInfo()
				.getUserName());

	}

	@Override
	protected void onResume() {
		super.onResume();
		ImDataCenter.getInstance().bindListener(this);
		// CrmDataCenter.getInstance().bindListener(this);
		View v = findViewById(R.id.btn_left);
		if (v != null) {
			v.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					areaOnTouchListener();
					openMenu();
				}
			});
		}
		handleMessage(ImDataCenter.MENU_REFRESH);
		// refreshMenuSelected();
	}

	@Override
	protected void onPause() {
		ImDataCenter.getInstance().unbindListener(this);
		super.onPause();
	}

	protected void refreshMenuSelected() {
		home.setSelected(MenuDomain.isClickable(MenuDomain.MenuItems.HOME));
		sensitive.setSelected(MenuDomain
				.isClickable(MenuDomain.MenuItems.SENSITIVE));
		acne.setEnabled(MenuDomain.isClickable(MenuDomain.MenuItems.ACNE));
		stain.setEnabled(MenuDomain.isClickable(MenuDomain.MenuItems.STAIN));

		dry.setSelected(MenuDomain.isOpen(MenuDomain.MenuItems.DRY));
		complex.setSelected(MenuDomain.isOpen(MenuDomain.MenuItems.COMPLEX));

	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		areaOnTouchListener();
		return super.dispatchTouchEvent(ev);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_MENU) {
			if (mLeftNav != null && mLeftNav.isDrawerOpened()) {
				closeMenu();
				return false;
			} else {
				openMenu();
				return false;
			}
		}
		if (keyCode == KeyEvent.KEYCODE_BACK && mLeftNav != null
				&& mLeftNav.isDrawerOpened()) {
			closeMenu();
			return false;
		}
		return super.onKeyDown(keyCode, event);

	}

	protected void openMenu() {
		if (mLeftNav != null && !mLeftNav.isDrawerOpened()) {
			mLeftNav.openDrawer();
		}
	}

	protected void closeMenu() {
		if (mLeftNav != null && mLeftNav.isDrawerOpened()) {
			mLeftNav.closeDrawer();
		}
	}

	@Override
	protected void onDestroy() {
		WinonaApplication.getInstance().removeActivity(this);
		super.onDestroy();
	}

	// 对滑动地方的监听
	protected void areaOnTouchListener() {
		hideKeyboard();
	}

	protected void hideKeyboard() {
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		if (imm != null && getCurrentFocus() != null) {
			imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
					InputMethodManager.HIDE_NOT_ALWAYS);
		}
	}

	protected void showKeyboard() {
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		if (imm != null && getCurrentFocus() != null) {
			imm.showSoftInputFromInputMethod(
					getCurrentFocus().getWindowToken(),
					InputMethodManager.SHOW_FORCED);
		}
	}

	@Override
	public void handleMessage(String type) {
		if (type.equals(ImDataCenter.MENU_REFRESH)) {

			if (ImDataCenter.getInstance().getPriNum() > 0) {
				mNavPrivateCount.setText(String.valueOf(ImDataCenter
						.getInstance().getPriNum()));
				mNavPrivateCount.setVisibility(View.VISIBLE);
			} else {
				mNavPrivateCount.setVisibility(View.GONE);
			}
		}

	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.left_userinfo_area:
			userinfo_area.setBackgroundColor(color.red);
			ll_home.setBackgroundResource(R.drawable.left_background);
			ll_conversation.setBackgroundResource(R.drawable.left_background);
			ll_academic.setBackgroundResource(R.drawable.left_background);
			ll_tool.setBackgroundResource(R.drawable.left_background);
			ll_set.setBackgroundResource(R.drawable.left_background);

			userinfo_area.setVisibility(View.VISIBLE);
			ll_home.setVisibility(View.INVISIBLE);
			ll_conversation.setVisibility(View.INVISIBLE);
			ll_academic.setVisibility(View.INVISIBLE);
			ll_tool.setVisibility(View.INVISIBLE);
			ll_set.setVisibility(View.INVISIBLE);

			break;
		case R.id.left_ll_home:
			ll_home.setBackgroundColor(color.red);
			userinfo_area.setBackgroundResource(R.drawable.left_background);
			ll_conversation.setBackgroundResource(R.drawable.left_background);
			ll_academic.setBackgroundResource(R.drawable.left_background);
			ll_tool.setBackgroundResource(R.drawable.left_background);
			ll_set.setBackgroundResource(R.drawable.left_background);

			ll_home.setVisibility(View.VISIBLE);
			userinfo_area.setVisibility(View.INVISIBLE);
			ll_conversation.setVisibility(View.INVISIBLE);
			ll_academic.setVisibility(View.INVISIBLE);
			ll_tool.setVisibility(View.INVISIBLE);
			ll_set.setVisibility(View.INVISIBLE);

			break;
		case R.id.left_ll_academic:
			ll_academic.setBackgroundColor(color.red);
			userinfo_area.setBackgroundResource(R.drawable.left_background);
			ll_home.setBackgroundResource(R.drawable.left_background);
			ll_conversation.setBackgroundResource(R.drawable.left_background);
			ll_tool.setBackgroundResource(R.drawable.left_background);
			ll_set.setBackgroundResource(R.drawable.left_background);

			ll_academic.setVisibility(View.VISIBLE);
			userinfo_area.setVisibility(View.INVISIBLE);
			ll_home.setVisibility(View.INVISIBLE);
			ll_conversation.setVisibility(View.INVISIBLE);
			ll_tool.setVisibility(View.INVISIBLE);
			ll_set.setVisibility(View.INVISIBLE);

			break;
		case R.id.left_ll_tool:
			ll_tool.setBackgroundColor(color.red);
			userinfo_area.setBackgroundResource(R.drawable.left_background);
			ll_home.setBackgroundResource(R.drawable.left_background);
			ll_academic.setBackgroundResource(R.drawable.left_background);
			ll_conversation.setBackgroundResource(R.drawable.left_background);
			ll_set.setBackgroundResource(R.drawable.left_background);

			ll_tool.setVisibility(View.VISIBLE);
			userinfo_area.setVisibility(View.INVISIBLE);
			ll_home.setVisibility(View.INVISIBLE);
			ll_academic.setVisibility(View.INVISIBLE);
			ll_conversation.setVisibility(View.INVISIBLE);
			ll_set.setVisibility(View.INVISIBLE);

			break;
		case R.id.left_ll_set:
			ll_set.setBackgroundColor(color.red);
			userinfo_area.setBackgroundResource(R.drawable.left_background);
			ll_home.setBackgroundResource(R.drawable.left_background);
			ll_academic.setBackgroundResource(R.drawable.left_background);
			ll_tool.setBackgroundResource(R.drawable.left_background);
			ll_conversation.setBackgroundResource(R.drawable.left_background);

			ll_set.setVisibility(View.VISIBLE);
			userinfo_area.setVisibility(View.INVISIBLE);
			ll_home.setVisibility(View.INVISIBLE);
			ll_academic.setVisibility(View.INVISIBLE);
			ll_tool.setVisibility(View.INVISIBLE);
			ll_conversation.setVisibility(View.INVISIBLE);

			break;

		}

	}

	protected void startActivity(Class<?> cls) {
		startActivity(cls, null);
	}

	public void startActivity(Class<?> cls, Object obj) {
		Intent intent = new Intent(this, cls);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		if (obj != null) {
			intent.putExtra("data", (Serializable) obj);
		}
		startActivity(intent);
		overridePendingTransition(R.anim.input, R.anim.out);
	}

	public void startActivityForResult(Class<?> cls, Object obj, int requestCode) {
		Intent intent = new Intent(this, cls);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		if (obj != null)
			intent.putExtra("data", (Serializable) obj);
		startActivityForResult(intent, requestCode);
		overridePendingTransition(R.anim.input, R.anim.out);
	}
}
